package net.sxnic.item.zzdw;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.sxinfo.core.spring25.Dao;
import net.sxinfo.core.spring25.DefaultManager;
import net.sxnic.item.ItemConstants;
import net.sxnic.item.zzdw.dao.ZzdwDao;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("zzdwManager")
public class DefaultZzdwManager extends DefaultManager<Zzdw, ZzdwDao, String>
		implements ZzdwManager {

	@Autowired
	private ZzdwDao zzdwDao;

	@Override
	protected Dao<Zzdw, String> getEntityDao() {
		return zzdwDao;
	}

	@Override
	public Zzdw findGldwByCatalogAlias(String catalogAlias) {
		return null;
	}

	@Override
	public Map<String, String> findZzdwCodeNameMap() {
		List<Zzdw> list = zzdwDao
				.findBySql("from Zzdw where catalogAlias=?  and cate='zzdw' order by xorder,deptCode",new Object[]{ItemConstants.SYSTEM_CALALOG_ALIAS});
		Map<String, String> map = new LinkedHashMap<String, String>();

		for (Zzdw z : list) {
			map.put(z.getDeptCode(), z.getDeptName());
		}

		return map;
	}

	@Override
	public Map<String, String> findZzdwMapByAlias(String catalogAlias) {
		return null;
	}

	@Override
	public Map<String, Map<String, String>> findZzdwMapByAlias() {
		// 找到所有的大类

		// 给每个大类生成相应的组织单位Map

		return null;
	}

	@Override
	public void initAllVerifyDetpMap() {
		ItemConstants.VERIFY_DEPT_MAP = new LinkedHashMap<String, String>();
		List<Zzdw> list = zzdwDao.getAll();
		for (Zzdw z : list) {
			ItemConstants.VERIFY_DEPT_MAP.put(z.getDeptCode(), z.getDeptName());
		}
	}

	@Override
	public Zzdw findByDeptCodeCatalogAlias(String deptCode, String catalogAlias) {
		DetachedCriteria dc = DetachedCriteria.forClass(Zzdw.class);

		dc.add(Restrictions.eq("deptCode", deptCode));
		dc.add(Restrictions.eq("catalogAlias", catalogAlias));

		List<Zzdw> list = zzdwDao.getByDetachedCriteria(dc);

		if (list == null || list.size() == 0) {
			return new Zzdw();
		}

		return list.get(0);
	}

}
